#!/bin/bash


function usage()
{
    echo "usage :cmd [params]"
    echo "cmd:"
    echo "  help|-help|--help|-h|--h"
    echo "  online|status|restart|start|stop|reset|autoreset dest [params]"
    echo "      dest:all|host.x|sid|minsid-maxsid"
}

if (($# < 2)); then
    usage
    exit 1
fi

###########################################################################
online() {
    ip=$1
    sid=$2
    path=`./config/get.sh cloudarena.path $sid`
    result=`ssh $ip "source ~/.bash_profile; 
        cd $path; 
        ./cloud_manage stop; 
        git reset --hard > /dev/null 2>&1; 
        git pull >/dev/null 2>&1; 
        node init_cloud_db.js; 
        ./cloud_manage start; 
        git log |head -n 1"`
    ./util/info.sh "$title" "finish" "$result"
}

status(){
    ip=$1
    sid=$2
    path=`./config/get.sh cloudarena.path $sid`
    result=`ssh $ip "source ~/.bash_profile; 
        cd $path; 
        ./cloud_manage status; 
        git log |head -n 1"`
    ./util/info.sh "$title" "finish" "$result"
}

restart(){
    ip=$1
    sid=$2
    path=`./config/get.sh cloudarena.path $sid`
    result=`ssh $ip "source ~/.bash_profile; 
        cd $path; 
        ./cloud_manage restart; 
        git log |head -n 1"`
    ./util/info.sh "$title" "finish" "$result"
}

start(){
    ip=$1
    sid=$2
    path=`./config/get.sh cloudarena.path $sid`
    result=`ssh $ip "source ~/.bash_profile; 
        cd $path; 
        ./cloud_manage start; 
        git log |head -n 1"`
    ./util/info.sh "$title" "finish" "$result"
}

stop(){
    ip=$1
    sid=$2
    path=`./config/get.sh cloudarena.path $sid`
    result=`ssh $ip "source ~/.bash_profile; 
        cd $path; 
        ./cloud_manage stop; 
        git log |head -n 1"`
    ./util/info.sh "$title" "finish" "$result"
}

reset() {
    ip=$1
    sid=$2

    path=`./config/get.sh cloudarena.path $sid`
    db_address=`./config/get.sh server.db_address cloudarena $sid`
    db_name=`./config/get.sh cloudarena.db_name $sid`

    mongo $db_address/$db_name ./mongo/remove_cloudarena_data.js
    result=`ssh $ip "source ~/.bash_profile; 
        cd $path; 
        git reset --hard > /dev/null 2>&1; 
        git pull >/dev/null 2>&1; 
        node init_cloud_db.js; 
        git log |head -n 1"`
    ./util/info.sh "$title" "finish" "$result"
}

autoreset() {
    ip=$1
    sid=$2

    stop $ip $sid
    reset $ip $sid
    start $ip $sid
}

mem() {
    ip=$1
    sid=$2
    echo -n "[$sid] "
    path=`./config/get.sh cloudarena.path $sid`
    ssh $ip "source ~/.bash_profile; 
        cd $path;
        if [ -f cloud.pid ]; then
            pid=\`cat cloud.pid\`
            top -b -n 1 -p \$pid | grep node
        else
            echo Lost
        fi;"
}

###########################################################################

cd $(dirname $(readlink -f $0));
process=`basename $0`

cmd=$1
dest=$2
title="$cmd $dest"

case "$cmd" in
    help|-help|--help|-h|--h)
        usage
        exit
    ;;
    online|status|restart|start|stop|reset|autoreset|mem) ;;
    *)
    ./util/info.sh "cmd $cmd" "unknown"
    usage
    exit 1
esac


if [ "all" == $dest ]; then
    hosts=(`./config/get.sh server.list cloudarena id`)
    for host in ${hosts[*]}; do
        ./$process $cmd host.$host $3 $4 $5
    done
elif [ ${dest:0:5} == "host." ]; then
    host=${dest:5}
    ip=`./config/get.sh server.host.$host.ip`
    if [ -z $ip ]; then
        ./util/error.sh "$cmd $dest" "invalid"
        exit 1
    fi

    host_sids=(`./config/get.sh server.sids cloudarena $host`)
    for sid in ${host_sids[*]}; do
        title="$cmd $sid"
        $cmd $ip $sid $3 $4 $5
    done

    title="$cmd $dest"
    if [ ${#host_sids[*]} -gt 0 ]; then
        ./util/info.sh "$cmd $dest" "finish"
    fi
else
    sid=$dest
    minsid=${sid%%-*}
    maxsid=${sid##*-}
    ((sid=minsid-1))
    while [ $sid -lt $maxsid ]; do
        ((sid=sid+1))
        title="$cmd $sid"
        ip=`./config/get.sh server.query cloudarena $sid ip`
        if [ -z $ip ]; then
            ./util/error.sh "$title" "invalid"
            exit 1
        fi

        $cmd $ip $sid $3 $4 $5
    done
fi
